using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesClaimsPendingForApproval : System.Web.UI.Page
{
    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;

    #region CurrentPageIndex
    protected int CurrentPageIndex
    {
        get
        {
            return (int)ViewState["CurrentPageIndex"];
        }

        set
        {
            ViewState["CurrentPageIndex"] = value;
        }
    }
    #endregion

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState];
        }

        set
        {
            ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState] = value;
        }
    }
    #endregion

    #region Events

    #region Page_Load
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (!IsPostBack)
        {
            this.CurrentPageIndex = 0;

            this.SwitchLanguage();
            this.PageComponentSetup();

            this.LoadData();

            DataView ResultView = new DataView(this.ResultTable);
            ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
            this.BindData(ResultView);
        } 
    }
    #endregion 

    #region DropDownList_SortBy_SelectedIndexChanged
    protected void DropDownList_SortBy_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataView ResultView = new DataView(this.ResultTable);

        if (this.DropDownList_ByClaimant.SelectedIndex > 0)
        {
            ResultView.RowFilter = "ClaimantInternalID = '" + this.DropDownList_ByClaimant.SelectedValue + "'";
        }

        ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
        this.BindData(ResultView, this.CurrentPageIndex);
    }
    #endregion

    #region DropDownList_SortOrder_SelectedIndexChanged
    protected void DropDownList_SortOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataView ResultView = new DataView(this.ResultTable);

        if (this.DropDownList_ByClaimant.SelectedIndex > 0)
        {
            ResultView.RowFilter = "ClaimantInternalID = '" + this.DropDownList_ByClaimant.SelectedValue + "'";
        }

        ResultView.Sort = this.DropDownList_SortBy.SelectedValue + " " + this.DropDownList_SortOrder.SelectedValue;
        this.BindData(ResultView, this.CurrentPageIndex);
    }
    #endregion

    #region DropDownList_ByClaimant_SelectedIndexChanged
    protected void DropDownList_ByClaimant_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    #endregion

    #region GridViewResult_PageIndexChanging
    protected void GridViewResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.CurrentPageIndex = e.NewPageIndex;
        this.DropDownList_SortBy_SelectedIndexChanged(sender, e);
    }
    #endregion

    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        // claimant, personnel direct report to
        using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
        {
            SystemUsers ClaimantsCollection = CurUserManager.GetUsersByApprover(this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID);            

            if (ClaimantsCollection != null)
            {
                foreach (SystemUser CurClaimant in ClaimantsCollection)
                {
                    this.DropDownList_ByClaimant.Items.Add(new ListItem(CurClaimant.DisplayName, CurClaimant.InternalID));
                }
            }
            else
            {
                this.CurLogManager.LogError("[GonzalesClaimsPendingForApproval.PageComponentSetup] : Generating Claimants by Approver : " + CurUserManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
            }
        }     
 
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {
                this.Label_Information.Text = CurLanguageFactory.DisplayValue("Information");
                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");
                this.Label_Remarks.Text = CurLanguageFactory.DisplayValue("Remarks");
               
                // claimant
                this.DropDownList_ByClaimant.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemShowAll"), ""));

                // sort by
               
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemDateSubmitted"), "DateSubmitted"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemClaimID"), "ClaimID"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemCategory"), "Category"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemPurpose"), "Purpose"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemClaimPeriod"), "ClaimPeriod"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemCurrency"), "LocalCurrency"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemAmount"), "Amount"));
                this.DropDownList_SortBy.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("ItemClaimant"), "Claimant"));        

                // sort order
                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("SortOrderASC"), "ASC"));
                this.DropDownList_SortOrder.Items.Add(new ListItem(CurLanguageFactory.DisplayValue("SortOrderDESC"), "DESC"));

                // GridView
                this.GridViewResult.Columns[0].HeaderText = CurLanguageFactory.DisplayValue("ItemDateSubmitted");
                this.GridViewResult.Columns[1].HeaderText = CurLanguageFactory.DisplayValue("ItemCategory");
                this.GridViewResult.Columns[2].HeaderText = CurLanguageFactory.DisplayValue("ItemClaimID");
                this.GridViewResult.Columns[3].HeaderText = CurLanguageFactory.DisplayValue("ItemPurpose");
                this.GridViewResult.Columns[4].HeaderText = CurLanguageFactory.DisplayValue("ItemClaimPeriod");
                this.GridViewResult.Columns[5].HeaderText = CurLanguageFactory.DisplayValue("ItemCurrency");
                this.GridViewResult.Columns[6].HeaderText = CurLanguageFactory.DisplayValue("ItemAmount");
                this.GridViewResult.Columns[7].HeaderText = CurLanguageFactory.DisplayValue("ItemClaimant");   

            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesClaimsPendingForApproval.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #endregion

    #region Data Operation

    protected void LoadData()
    {
        using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
        {
            ClaimHeaders ResultObj = CurClaimManager.GetClaimsByApprover(this.CurrentPrivateSession.CurrentLoggedOnUser.InternalID);

            if (ResultObj != null)
            {
                this.ResultTable = new DataTable();
                ResultTable.Columns.Add(new DataColumn("DateSubmitted", Type.GetType("System.DateTime")));
                ResultTable.Columns.Add(new DataColumn("DateSubmittedInString", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("Category", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("ClaimID", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("Purpose", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("LocalCurrency", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("Amount", Type.GetType("System.Decimal")));
                ResultTable.Columns.Add(new DataColumn("AmountInStr", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("ClaimPeriod", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("Claimant", Type.GetType("System.String")));
                ResultTable.Columns.Add(new DataColumn("ClaimantInternalID", Type.GetType("System.String")));

                DataRow RowObj = null;

                for (int x = 0; x < ResultObj.Count; x++)
                {
                    RowObj = ResultTable.NewRow();

                    GonzalesClaimHeader CurClaimHeader = (GonzalesClaimHeader)ResultObj[x];

                    RowObj["DateSubmitted"] = ResultObj[x].SubmittedOn;
                    RowObj["DateSubmittedInString"] = "<a href='GonzalesClaimsPendingForApprovalDetail.aspx?id=" + ResultObj[x].InternalID + "' class='GridViewLink'>" + SwissArmy.DateConvert(ResultObj[x].SubmittedOn, this.CurrentPrivateSession.DateFormatInUse, "/") + "</a>";
                    RowObj["Category"] = ClaimManager.GetClaimCategoryInString(CurClaimHeader.Category);
                    RowObj["ClaimID"] = ResultObj[x].ClaimID;
                    RowObj["Purpose"] = ResultObj[x].Reasons;
                    RowObj["LocalCurrency"] = CurClaimHeader.LocalCurrency.InternalID;
                    RowObj["Amount"] = ResultObj[x].TotalClaimedAmount;
                    RowObj["AmountInStr"] = ResultObj[x].TotalClaimedAmount.ToString(GonzalesGeneralVariables.CurrencyFormat);
                    if (ResultObj[x].ProjectedOn != DateTime.MinValue)
                        RowObj["ClaimPeriod"] = ResultObj[x].ProjectedOn.Year.ToString() + " - " + ResultObj[x].ProjectedOn.Month.ToString();//ResultObj[x].ProjectedOn.ToString("MMMM") + " " + ResultObj[x].ProjectedOn.Year.ToString();
                    else
                        RowObj["ClaimPeriod"] = "";
                    RowObj["Claimant"] = ResultObj[x].ClaimedBy.DisplayName + " (" + ResultObj[x].ClaimedBy.EmailID + ")"; ;
                    RowObj["ClaimantInternalID"] = ResultObj[x].ClaimedBy.InternalID;

                    ResultTable.Rows.Add(RowObj);
                }

            }
        }
    }

    private void BindData(DataView result)
    {
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    private void BindData(DataView result, int pageindex)
    {
        this.GridViewResult.PageIndex = pageindex;
        this.GridViewResult.DataSource = result;
        this.GridViewResult.DataBind();
    }

    #endregion

}
